function Animation(cfg) {
  for (var attr in cfg ) {
    this[attr] = cfg[attr];
  }
}

Animation.prototype = {
  constructor: Animation,
  frames: null,
  frameCount: -1,
  img: null,
  currentFrame: null ,
  currentFrameIndex: -1,
  currentFramePlayed: -1,

  init: function() {
    this.frames = this.frames || [];
    this.frameCount = this.frames.length;
    this.setFrame(0);
  },

  // Set current frame.
  setFrame: function(index) {
    this.currentFrameIndex = index;
    this.currentFrame = this.frames[index];
    this.currentFramePlayed = 0;
  },

  update: function(deltaTime) {
    if (this.currentFramePlayed >= this.currentFrame.duration) {
      if (this.currentFrameIndex >= this.frameCount - 1) {
        this.currentFrameIndex = 0;
      } else {
        this.currentFrameIndex++;
      }
      this.setFrame(this.currentFrameIndex);
    } else {
      this.currentFramePlayed += deltaTime;
    }
  },
  
  draw: function(context, dx, dy) {
    var frame = this.currentFrame;
    context.drawImage(this.img, frame.sx , frame.sy, frame.sw, frame.sh, dx, dy,
      frame.dw, frame.dh);
  }
};

